package Servlet;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.apache.catalina.User;
import Dao.UserDao;

public class SelectUser {
	public void save(int id, String JDBC_URL, String JDBC_USER, String JDBC_PASSWORD, User user) throws Exception{
		Connection con = null;
		User user = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			con = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
			ps = con.prepareStatement("select * from users where id=?");
			ps.setInt(1, id);
			ps.executeQuery();
			if(rs.next()) {
				user = new User();
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
			}
		}catch(Exception e){
			throw new Exception("数据库异常:" + e.getMessage());
		}finally {
			if(rs != null) {
				rs.close();
			}
			if(ps != null) {
				ps.close();
			}
			if(con != null) {
				con.close();
			}
		}
	}
	public static void main(String[] args) {
		User user = new User();
		user.setUsername("张三");
		user.setPassword("123456");
		UserDao userDAO = new UserDao();
		try {
			userDAO.save(user);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}
